package com.ddl.kafka.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitConfig {

	/**
	 * 直连配置
	 */
	public static class DirectExchangeDemoConfiguration {

		@Value("${amqp.testQueueName}")
		private String queueName;

		@Value("${amqp.testExchange}")
		private String testExchange;

		@Value("${amqp.testRoutingKey}")
		private String testRoutingKey;

		@Value("${amqp.deadQueueName}")
		private String deadQueueName;

		@Value("${amqp.deadRoutingKey}")
		private String deadRoutingKey;

		/**
		 * 创建一个队列
		 */
		@Bean
		Queue queue() {

			Map<String, Object> args = new HashMap<>(3);
			// 绑定死信交换器
			args.put("x-dead-letter-exchange", testExchange);
			// 绑定死信路由键
			args.put("x-dead-letter-routing-key", deadRoutingKey);

			/*return QueueBuilder.durable(queueName) // durable: 是否持久化
					.exclusive() // exclusive: 是否排它
					.autoDelete() // autoDelete: 是否自动删除
					//.withArguments(args)
					.build();*/
			return new Queue(queueName, true, false, false, args);
			/*return QueueBuilder.durable(queueName) // durable: 是否持久化
					.exclusive() // exclusive: 是否排它
					.autoDelete() // autoDelete: 是否自动删除
					.build();*/
		}

		/**
		 * 创建死信队列
		 */
		@Bean
		public Queue deadQueue() {
			return new Queue(deadQueueName, // Queue 名字
					true, // durable: 是否持久化
					false, // exclusive: 是否排它
					false); // autoDelete: 是否自动删除
		}

		/**
		 * 创建一个直连交换机
		 */
		@Bean
		DirectExchange exchange() {
			return new DirectExchange(testExchange, true, false);
		}

		/**
		 * 将新建的队列和exchange绑定
		 */
		@Bean
		Binding binding() {
			return BindingBuilder.bind(queue())
					.to(exchange())
					.with(testRoutingKey);
		}

		/**
		 * 创建死信队列绑定这个直连交换机
		 */
		@Bean
		public Binding deadBinding() {
			return BindingBuilder.bind(deadQueue()).to(exchange()).with(deadRoutingKey);
		}
	}

	/*@Bean
	public BatchingRabbitTemplate batchRabbitTemplate(ConnectionFactory connectionFactory) {
		// 创建 BatchingStrategy 对象，代表批量策略
		int batchSize = 16384; // 超过收集的消息数量的最大条数。
		int bufferLimit = 33554432; // 每次批量发送消息的最大内存
		int timeout = 10000; // 超过收集的时间的最大等待时长，单位：毫秒
		BatchingStrategy batchingStrategy = new SimpleBatchingStrategy(batchSize, bufferLimit, timeout);

		// 创建 TaskScheduler 对象，用于实现超时发送的定时器
		TaskScheduler taskScheduler = new ConcurrentTaskScheduler();

		// 创建 BatchingRabbitTemplate 对象
		BatchingRabbitTemplate batchTemplate = new BatchingRabbitTemplate(batchingStrategy, taskScheduler);
		batchTemplate.setConnectionFactory(connectionFactory);
		return batchTemplate;
	}*/

	@Bean(name = "consumerBatchContainerFactory")
	public SimpleRabbitListenerContainerFactory consumerBatchContainerFactory(
			SimpleRabbitListenerContainerFactoryConfigurer configurer, ConnectionFactory connectionFactory) {
		// 创建 SimpleRabbitListenerContainerFactory 对象
		SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
		configurer.configure(factory, connectionFactory);
		// <X> 额外添加批量消费的属性
		factory.setDeBatchingEnabled(true);
		return factory;
	}

	/**
	 * 广播配置
	 */
	public static class TopicExchangeDemoConfiguration {

		@Value("${amqp.topicQueueName}")
		private String topicQueueName;

		@Value("${amqp.topicExchange}")
		private String topicExchange;

		// 创建 Queue
		@Bean
		public Queue topicQueue() {
			return new Queue(topicQueueName, // Queue 名字
					true, // durable: 是否持久化
					false, // exclusive: 是否排它
					false); // autoDelete: 是否自动删除
		}

		// 创建 Topic Exchange
		@Bean
		public TopicExchange topicExchange() {
			return new TopicExchange(topicExchange,
					true,  // durable: 是否持久化
					false);  // exclusive: 是否排它
		}

		// 创建 Binding
		@Bean
		public Binding topicBinding() {
			return BindingBuilder.bind(topicQueue()).to(topicExchange()).with("#.send.email");
		}

	}
}
